﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.IO;
using System.Windows.Forms;

#endregion


namespace ESF.WinForms
{
	public class OutputRickTextBoxWriter : StringWriter	
    {
        #region Miembros
        
        private RichTextBox TextBox;
 
		#endregion
		
 
		#region Constructor
		
		/// <summary>
        /// Create a new object to redirect the output
        /// </summary>
        /// <param name="outFileName">
        /// The name of the file to capture console output
        /// </param>
        public OutputRickTextBoxWriter (ref RichTextBox pTextBox)
        {
            TextBox = pTextBox;
        }
        
        #endregion
        
        
        #region Funciones
 
        public override void Write (string pText)
		{
			TextBox.Invoke (new Action<string> (UpdateTextBox), pText);
		}
		
		public void UpdateTextBox (string pText) 
		{
			TextBox.AppendText (pText);
			TextBox.Refresh();
			TextBox.ScrollToCaret ();
		}
		
		
		public override void WriteLine(string pText)
		{
			Write (pText + Environment.NewLine);
		}
        
        #endregion
    }
}
